<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //promise.race简单原理
        let a = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve('成功')
            }, 2000)
        })
        let b = new Promise((resolve, reject) => {
            reject('失败')
        })
        // Promise.race([a, b]).then(res => {
        //     console.log(res)
        // }).catch(err => {
        //     console.log(err)
        // })
        function myPromise(list) {
            return new Promise((reslove, reject) => {
                const len = list.length
                let reslutArr = new Array(len)
                let num = 0
                list.forEach((item, index) => {
                    Promise.resolve(item).then((val) => {
                        return reslove(val)
                        // num++
                        // console.log(num)
                        // reslutArr[index] = val
                        // if (num === len) {
                        //     return reslove(reslutArr)
                        // }
                    }, (reason) => {
                        return reject(reason)
                    })
                })
            })
        }
        myPromise([a, b]).then(res => {
            console.log(res)
        }).catch(err => {
            console.log(err)
        })
    </script>
</body>

</html>